﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace Placid.PivotSourceImpl
{
    public class PivotSourceColumnsCollection : IEnumerable<PivotSourceColumn>
    {
        private PivotSource _container;
        private List<PivotSourceColumn> _columns;
        private Dictionary<int, PivotSourceColumn> _columnsByIndex;
        private Dictionary<string, PivotSourceColumn> _columnsByName;

        public PivotSourceColumnsCollection(PivotSource container)
        {
            if(container != null) {
                _container = container;
            } else throw new Exception("IPvSource Container is a null reference.");

            _columns = new List<PivotSourceColumn>();
            _columnsByIndex = new Dictionary<int, PivotSourceColumn>();
            _columnsByName = new Dictionary<string, PivotSourceColumn>();

            BuildColumns();
        }

        private void BuildColumns()
        {
            PivotSourceColumn pcolumn;
            DataColumn col;

            _columns.Clear();
            _columnsByIndex.Clear();
            _columnsByName.Clear();

            for(int colIndex = 0; colIndex < _container._source.Columns.Count; colIndex++) {
                col = _container._source.Columns[colIndex];
                pcolumn = new PivotSourceColumn(col.ColumnName, colIndex, _container, colIndex);
                _columns.Add(pcolumn);
                _columnsByIndex.Add(colIndex, pcolumn);
                _columnsByName.Add(col.ColumnName, pcolumn);
            }
        }

        public void Clear()
        {
            _columns.Clear();
            _columnsByIndex.Clear();
            _columnsByName.Clear();
        }

        public PivotSourceColumn this[int index]
        {
            get
            {
                if(_columnsByIndex != null && _columnsByIndex.ContainsKey(index)) return _columnsByIndex[index];
                else return null;
            }
        }

        public PivotSourceColumn this[string name]
        {
            get
            {
                if(_columnsByName != null && _columnsByName.ContainsKey(name)) return _columnsByName[name];
                else return null;
            }
        }

        public int Count
        {
            get
            {
                if(_columns != null) return _columns.Count;
                else return 0;
            }
        }

        #region IEnumerable<DSrcColumn> Members

        public IEnumerator<PivotSourceColumn> GetEnumerator()
        {
            if(_columns != null) {
                return _columns.GetEnumerator();
            } else {
                return null;
            }
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            if(_columns != null) {
                return _columns.GetEnumerator();
            } else {
                return null;
            }
        }

        #endregion
    }
}
